/**
 * Created by nena on 2018/3/22.
 */
let errCode = require("../utils/errCode");
let acdManager = require("../server/acdManager");
let Ajv = require("ajv");

let cb = function (cmd, mreply, err, reply) {
  if (cmd !== "requestWaiting") {
    console.log("err", err);
    console.log(cmd, " reply: ", reply);
  }
  if (err) return mreply(null, err);
  let ret = errCode.getErrInfo(errCode.SUCCESS);
  ret.result = reply;
  mreply(null, ret);
};

const schema = {
  type: "object",
  properties: {
    groups: { type: "array", items: { type: "string" } },
    agents: { type: "array", items: { type: "string" } },
    adds: { type: "array", items: { type: "string" } },
    deletes: { type: "array", items: { type: "string" } },
    group: { type: "array", items: { type: "string" } },
    agent: { type: "string" },
    advance: { type: "boolean" },
    agentId: { type: "string" },
    id: { type: "string" },
    newId: { type: "string" },
  },
};
let ajv = new Ajv();
let validate = ajv.compile(schema);

module.exports = function acdmodel(options) {
  this.add("cmd:onlineAgent", async function (msg, mreply) {
    console.log("cmd:onlineAgent msg:", msg);
    if (!msg || !msg.domain || !msg.agentId)
      return cb(
        "onlineAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "onlineAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    msg.groups = msg.groups ? msg.groups : [];

    let reply = await acdManager.onlineAgent(
      msg.domain,
      msg.agentId,
      msg.groups
    );
    cb("onlineAgent", mreply, null, reply);
  });

  this.add("cmd:offlineAgent", async function (msg, mreply) {
    console.log("cmd:offlineAgent msg:", msg);
    if (!msg || !msg.domain || !msg.agentId)
      return cb(
        "offlineAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "offlineAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.offlineAgent(msg.domain, msg.agentId);
    cb("offlineAgent", mreply, null, reply);
  });

  this.add("cmd:updateAgent", async function (msg, mreply) {
    console.log("cmd:updateAgent msg:", msg);
    if (!msg || !msg.domain || !msg.agentId)
      return cb(
        "updateAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "updateAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    msg.groups = msg.groups ? msg.groups : [];
    let reply = await acdManager.updateAgent(
      msg.domain,
      msg.agentId,
      msg.groups
    );
    cb("updateAgent", mreply, null, reply);
  });

  this.add("cmd:clearAgentStatus", async function (msg, mreply) {
    console.log("cmd:clearAgentStatus msg:", msg);
    if (!msg || !msg.domain || !msg.agentId)
      return cb(
        "clearAgentStatus",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "clearAgentStatus",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.clearAgentStatus(msg.domain, msg.agentId);
    cb("clearAgentStatus", mreply, null, reply);
  });

  this.add("cmd:setAgentStatus", async function (msg, mreply) {
    console.log("cmd:setAgentStatus msg:", msg);
    if (!msg || !msg.domain || !msg.agentId || !msg.requestId)
      return cb(
        "setAgentStatus",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "setAgentStatus",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.setAgentStatus(
      msg.domain,
      msg.agentId,
      msg.requestId
    );
    cb("setAgentStatus", mreply, null, reply);
  });

  this.add("cmd:addRequest", async function (msg, mreply) {
    console.log("cmd:addRequest msg:", msg);
    if (!msg || !msg.domain || !msg.id || !msg.group)
      return cb(
        "addRequest",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "addRequest",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.addRequest(
      msg.domain,
      msg.id,
      msg.group,
      msg.agent,
      msg.advance
    );
    cb("addRequest", mreply, null, reply);
  });

  this.add("cmd:deleteRequest", async function (msg, mreply) {
    console.log("cmd:deleteRequest msg:", msg);
    if (!msg || !msg.domain || !msg.id || !msg.group)
      return cb(
        "deleteRequest",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "deleteRequest",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.deleteRequest(
      msg.domain,
      msg.id,
      msg.newId,
      msg.group,
      msg.agentId
    );
    cb("deleteRequest", mreply, null, reply);
  });

  this.add("cmd:takeRequest", async function (msg, mreply) {
    console.log("cmd:takeRequest msg:", msg);
    if (!msg || !msg.domain || !msg.agentId)
      return cb(
        "takeRequest",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "takeRequest",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.takeRequest(msg.domain, msg.agentId);
    cb("takeRequest", mreply, null, reply);
  });

  this.add("cmd:requestWaiting", async function (msg, mreply) {
    // console.log("cmd:requestWaiting msg:", msg)
    if (!msg || !msg.domain || !msg.id || !msg.group)
      return cb(
        "requestWaiting",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "requestWaiting",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.requestWaiting(msg.domain, msg.id, msg.group);
    cb("requestWaiting", mreply, null, reply);
  });

  this.add("cmd:getFreeAgents", async function (msg, mreply) {
    if (!msg || !msg.domain || !msg.group)
      return cb(
        "getFreeAgents",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "getFreeAgents",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.getFreeAgents(msg.domain, msg.group);
    cb("getFreeAgents", mreply, null, reply);
  });

  this.add("cmd:checkFreeAgent", async function (msg, mreply) {
    if (!msg || !msg.domain || !msg.agent)
      return cb(
        "checkFreeAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "checkFreeAgent",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.checkFreeAgent(msg.domain, msg.agent);
    cb("checkFreeAgent", mreply, null, reply);
  });

  this.add("cmd:addGroup", async function (msg, mreply) {
    console.log("cmd:addGroup msg:", msg);
    if (!msg || !msg.domain || !msg.id)
      return cb("addGroup", mreply, errCode.getErrInfo(errCode.ERR_PARAMETER));
    if (!validate(msg))
      return cb(
        "addGroup",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    msg.agents = msg.agents ? msg.agents : [];
    let reply = await acdManager.addGroup(msg.domain, msg.id, msg.agents);
    cb("addGroup", mreply, null, reply);
  });

  this.add("cmd:deleteGroup", async function (msg, mreply) {
    console.log("cmd:deleteGroup msg:", msg);
    if (!msg || !msg.domain || !msg.groups)
      return cb(
        "deleteGroup",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "deleteGroup",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    let reply = await acdManager.deleteGroup(msg.domain, msg.groups);
    cb("deleteGroup", mreply, null, reply);
  });

  this.add("cmd:updateGroup", async function (msg, mreply) {
    console.log("cmd:updateGroup msg:", msg);
    if (!msg || !msg.domain || !msg.id)
      return cb(
        "updateGroup",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER)
      );
    if (!validate(msg))
      return cb(
        "updateGroup",
        mreply,
        errCode.getErrInfo(errCode.ERR_PARAMETER_UNAVAILABLE)
      );

    msg.adds = msg.adds ? msg.adds : [];
    msg.deletes = msg.deletes ? msg.deletes : [];
    let reply = await acdManager.updateGroup(
      msg.domain,
      msg.id,
      msg.adds,
      msg.deletes
    );
    cb("updateGroup", mreply, null, reply);
  });
};
